package com.sxx.mall.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.conditions.query.QueryChainWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sxx.commons.utils.PageUtils;
import com.sxx.commons.utils.Query;
import com.sxx.mall.product.dao.AttrGroupDao;
import com.sxx.mall.product.dao.SkuInfoDao;
import com.sxx.mall.product.dao.SkuSaleAttrValueDao;
import com.sxx.mall.product.entity.SkuImagesEntity;
import com.sxx.mall.product.entity.SkuInfoEntity;
import com.sxx.mall.product.entity.SpuInfoDescEntity;
import com.sxx.mall.product.service.SkuImagesService;
import com.sxx.mall.product.service.SkuInfoService;
import com.sxx.mall.product.service.SpuInfoDescService;
import com.sxx.mall.product.vo.ItemVO;
import com.sxx.mall.product.vo.SkuItemSaleAttrVo;
import com.sxx.mall.product.vo.SpuItemGroupAttrVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ThreadPoolExecutor;

@Service("skuInfoService")
public class SkuInfoServiceImpl extends ServiceImpl<SkuInfoDao, SkuInfoEntity> implements SkuInfoService {

    @Autowired
    private SkuImagesService skuImagesService;

    @Autowired
    AttrGroupDao attrGroupDao;

    @Autowired
    SkuSaleAttrValueDao skuSaleAttrValueDao;
    @Autowired
    private SpuInfoDescService spuInfoDescService;
    @Autowired
    ThreadPoolExecutor threadPoolExecutor;
    @Autowired
    SkuInfoDao skuInfoDao;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SkuInfoEntity> page = this.page(
                new Query<SkuInfoEntity>().getPage(params),
                new QueryWrapper<SkuInfoEntity>()
        );

        return new PageUtils(page);
    }

//    &page=1&limit=10&key=&catelogId=0&brandId=0&min=0&max=0
    @Override
    public PageUtils queryPageByCondition(Map<String, Object> params) {
        QueryWrapper<SkuInfoEntity> wrapper = new QueryWrapper<>();
        //关键字条件查询
        String key = (String) params.get("key");
        if (!StringUtils.isEmpty(key)){
            wrapper.eq("sku_id",key)
                    .or().like("sku_name",key)
                    .or().like("sku_desc",key);
        }
        //类别条件查询
        String catalogId = (String) params.get("catelogId");
        if (!StringUtils.isEmpty(catalogId) && !"0".equalsIgnoreCase(catalogId)){
            wrapper.eq("catalog_id",catalogId);
        }
        //品牌条件查询
        String brandId = (String) params.get("brandId");
        if (!StringUtils.isEmpty(brandId) && !"0".equalsIgnoreCase(brandId)){
            wrapper.eq("brand_id",brandId);
        }
        //价格区间条件查询
        String min = (String) params.get("min");
        if (!StringUtils.isEmpty(min)){
            wrapper.ge("price",min);
        }
        String max = (String) params.get("max");
        if (!StringUtils.isEmpty(max)){
            BigDecimal bigDecimal = new BigDecimal(max);
            if (bigDecimal.compareTo(new BigDecimal(0)) == 1){
                //判断max大于0
                wrapper.le("price",max);
            }
        }

        IPage<SkuInfoEntity> page = this.page(
                new Query<SkuInfoEntity>().getPage(params),
                wrapper
        );

        return new PageUtils(page);
    }

    @Override
    public List<SkuInfoEntity> getSkuBySpuId(Long spuId) {
        List<SkuInfoEntity> skus = this
                .list(new QueryWrapper<SkuInfoEntity>()
                        .eq("spu_id", spuId));
        return skus;
    }

    @Override
    public ItemVO showItem(Long skuId) throws ExecutionException, InterruptedException {
        ItemVO itemVO = new ItemVO();
        CompletableFuture<SkuInfoEntity> skuInfoFuture = CompletableFuture.supplyAsync(() -> {
            //1.sku的基本信息 pms_sku_info
            SkuInfoEntity skuInfoEntity = this.getById(skuId);
            itemVO.setInfo(skuInfoEntity);
            return skuInfoEntity;
        }, threadPoolExecutor);

        CompletableFuture<Void> saleAttrFuture = skuInfoFuture.thenAcceptAsync((ref) -> {
            //3.获取spu中销售属性的组合
            List<SkuItemSaleAttrVo> saleAttrs = skuSaleAttrValueDao.getSkuSaleAttrBySpuId(ref.getSpuId());
            itemVO.setSaleAttrs(saleAttrs);
        }, threadPoolExecutor);

        CompletableFuture<Void> spuDescFuture = skuInfoFuture.thenAcceptAsync((ref) -> {
            //4.获取spu的介绍
            List<SpuInfoDescEntity> spuDescList = spuInfoDescService
                    .list(new QueryWrapper<SpuInfoDescEntity>()
                            .eq("spu_id", ref.getSpuId()));
            itemVO.setDesc(spuDescList);
        }, threadPoolExecutor);

        CompletableFuture<Void> baseAttrFuture = skuInfoFuture.thenAcceptAsync((ref) -> {
            //5.获取spu的规格参数
            List<SpuItemGroupAttrVo> spuItemGroupAttrVo = attrGroupDao.getSpuBaseAttr(ref.getSpuId(), ref.getCatalogId());
            itemVO.setBaseAttrs(spuItemGroupAttrVo);
        }, threadPoolExecutor);

        CompletableFuture<Void> skuImgsFuture = CompletableFuture.runAsync(() -> {
            //2.sku的图片信息pms_sku_images
            List<SkuImagesEntity> skuImgs = skuImagesService
                    .list(new QueryWrapper<SkuImagesEntity>()
                            .eq("sku_id", skuId));
            itemVO.setImages(skuImgs);
        }, threadPoolExecutor);
        CompletableFuture.allOf(saleAttrFuture,spuDescFuture,baseAttrFuture,skuImgsFuture).get();
        return itemVO;
    }

    @Override
    public List<String> getSkuSaleAttrs(Long skuId) {

        return this.skuInfoDao.getSkuSaleAttrs(skuId);
    }

}